<?php
/**
 * 卡片产品
 * User: Acer
 * Date: 2022/7/9
 * Time: 2:22
 */
namespace app\card\controller;

use app\card\service\CardProService;
use app\card\service\CardService;
use app\card\service\CardTypeService;
use app\core\MyController;
class CardPro extends MyController
{
    protected $noNeedAction = ['export_card_pro']; // 不需要校验控制器，通道
    // 操作前置
    protected $beforeActionList = ['my_initialize'];

    // 操作前置方法
    public function my_initialize()
    {
//        $this->is_admin();  // 权限检查
    }
    // 创建卡片产品[批量创建]
    public function add_card_pro(){
        $data = input();
        if( empty($data['cid']) || empty($data['number']) )  json_err( -201);
        $res = (new CardProService())->s_add_card_pro($data,$this->userId);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 修改卡片产品
    public function state_card_pro(){
        $data = input();
        if( empty($data['pid']) )  json_err( -201);
        $res = (new CardProService())->s_state_card_pro($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }
    // 卡片产品列表
    public function card_pro_list(){
        $data = input();
        // 页数
        if(!isset($data['page']))  json_err( -201);
        // 当前页数
        if(!isset($data['page_number'])) $data['page_number'] = 10;

        // 查询
        $like =[];
        // 卡片产品code
        if( isset($data['code']) && $data['code'] )  $like['code'] = $data['code'];

        // 卡片名称id
        if( isset($data['cid']) && $data['cid'] )  $like['cid'] = $data['cid'];

        // 卡片状态
        if( isset($data['ActiveVaue']) && $data['ActiveVaue'] )  $like['ActiveVaue'] = $data['ActiveVaue'];
        // 卡片产品codes
        if( isset($data['codeRange']) && $data['codeRange'] )  $like['codeRange'] = $data['codeRange'];

        $res = (new CardProService())->s_card_pro_list($data['page'],$data['page_number'] > 0 ? $data['page_number'] : 1,$like);
        if($res) _action_result(200,$res,'操作成功！');
        json_err( -205);
    }
    // 导出卡片产品
    public function export_card_pro(){
        $data = input();
        if(!isset($data['cid']) || !isset($data['token']))  json_err( -201);
//        $uid = $this->checkToken($data['token']);
        $like =[];
        // 卡片产品code
        if( isset($data['code']) && $data['code'] ) {
            $like = [
                'code'=>$data['code']
            ];
        }
        // 卡片状态
        if( isset($data['ActiveVaue']) && $data['ActiveVaue'] ) {
            $like = [
                'ActiveVaue'=>$data['ActiveVaue']
            ];
        }
        // 获取当前卡片名称
        $cardInfo =  (new CardService())->getCardInfo($data['cid']);
        if(!$cardInfo) json_err(-204,['卡片信息，']);
        $cardInfo = obj_to_arr($cardInfo);
        // 获取卡片类型
        $card_type = (new CardTypeService())->s_card_type_info($cardInfo['ct_id']);
        if(!$card_type) json_err(-204,['卡片类型信息，']);
        $card_type = obj_to_arr($card_type);
        $list = (new CardProService())->s_export_card_pro($like,$data['cid']);
        $head = ['卡片编号','卡片密码','状态','是否激活','失效期','姓名','手机号','身份证号','客户经理手机号'];
        exportExcel($list, '('.$cardInfo['name'].'_'.$card_type['card_name'].')'.date('Y-m-d'), $head, 10);
        unset($list);
        exit();
    }
    // 根据卡片产品，获取用户成员
    public function get_card_users(){
        $data = input();
        if(!isset($data['pid']))  json_err( -201);
        $CardProService = new CardProService();
        $res = $CardProService->s_get_card_users($data['pid']);
        _action_result(200,$res,'操作成功！');
    }

    // 卡片解绑
    public function card_pro_unbound(){
        $data = input();
        if(empty($data['pid'])|| empty($data['type'])|| empty($data['did']))  json_err( -201);
        $CardProService = new CardProService();
        $res = $CardProService->s_card_pro_unbound($data['pid'],$data['type'],$data['did']);
        if($res)  _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 设置出库和归属地
    public function set_outbound(){
        $data = input();
        if(empty($data['pid']))  json_err( -201);
        $CardProService = new CardProService();
        if(!isset($data['buyer'])) $data['buyer']='';
        $res = $CardProService->set_outbound($data['pid'],$data['outbound'],$data['region'],$data['buyer'],$this->userId);
        if($res)  _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 设置备注
    public function set_notes(){
        $data = input();
        if(empty($data['pid'])||empty($data['notes']))  json_err( -201);
        $CardProService = new CardProService();
        $res = $CardProService->set_notes($data['pid'],$data['notes']);
        if($res)  _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 金牌卡后台点出库后，180天就会过期，如有的大客户放过期了，方便后续延时用
    public function delay_outbound(){
        $data = input();
        if(empty($data['pid'])||empty($data['num']))  json_err( -201);
        $res = (new CardProService())->delay_outbound($data['pid'],$data['num'],$this->userId);
        if($res)  _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    // 获取操作日志
    public function get_logger(){
        $data = input();
        if(!isset($data['page'])) json_err( -201);
        if(!isset($data['page_number'])) $data['page_number'] = 10;
        if(!isset($data['link_id']) or !isset($data['type'])) json_err( -201);

        $res = (new CardProService())->get_logger($data['page'],$data['page_number'] > 0 ? $data['page_number'] : 1, $data['link_id'], $data['type']);
        if($res) _action_result(200,$res,'操作成功！');
        json_err( -205);
    }
}
